const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_list: [],
    add_url: '',
    index_url: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getRenlingForm", function (res) {
      var data = res.data;
      // var form = data.form;
      that.setData({
        shop_id: options.shop_id,
        input_list: data.form,
        add_url: data.add_url,
        index_url: data.index_url,
        token: token
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindChange: function (e) {
    // console.log(e);
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        if (data.value == 'value1') {
          list[idx].value1 = e.detail.value;
        } else if (data.value == 'value2') {
          list[idx].value2 = e.detail.value;
        } else {
          list[idx].value = e.detail.value;
        }
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindMultiPickerColumnChange: function (e) {
    if (e.detail.column != 0) {
      return false;
    }
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        var index = e.detail.value;
        console.log(e);
        list[idx].range[1] = item.cates[index].sub_range;
        list[idx].value[0] = index;
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/upload?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              console.log(out);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                list.forEach(function (value, index) {
                  console.log(value);
                  console.log(data);
                  if (value.title == data.title) {
                    var pics = list[index].pics;
                    var file_name = "https://www.58zltc.com/Uploads/" + out.file_name;
                    if (data.num > 1) {
                      pics.push(file_name);
                    } else {
                      pics = [file_name];
                    }
                    list[index].pics = pics;
                  }
                })
                that.setData({
                  input_list: list
                })
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  formSubmit: function (e) {
    // console.log(e);
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    let that = this;
    data.input_list.forEach(function (item, idx) {
      if (item.type == 'picker1') {
        post_data[item.name] = item.range[item.value];
      } else if (item.type == 'picker2' || item.type == 'picker3') {
        post_data[item.name[0]] = item.range[0][item.value[0]];
        post_data[item.name[1]] = item.range[1][item.value[1]];
      } else if (item.type == 'date') {
        post_data[item.name] = item.value;
      } else if (item.type == 'time') {
        post_data[item.name] = item.value;
      } else if (item.type == 'time2') {
        post_data[item.name1] = item.value1;
        post_data[item.name2] = item.value2;
      } else if (item.type == 'tag') {
        var tags = '';
        item.tags.forEach(function (vo, key) {
          if (key == 0) {
            tags += vo.title;
          } else {
            tags += ',' + vo.title;
          }
        })
        post_data[item.name] = tags;
      } else if (item.type == 'pic') {
        post_data[item.name] = item.pics;
        // post_data.imgurl = item.pics[0];
      }
    })
    post_data.shop_id = data.shop_id;
    post_data.openid = app.globalData.openid;
    // if (!data.latitude) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请在地图中选择店铺所在位置！',
    //     showCancel: false,
    //     confirmText: '马上选择',
    //     success: function (res) {
    //       wx.chooseLocation({
    //         success: function (res) {
    //           that.setData({
    //             latitude: res.latitude,
    //             longitude: res.longitude
    //           })
    //         }
    //       })
    //     }
    //   })
    //   wx.hideLoading();
    //   return false;
    // }
    // post_data.latitude = data.latitude;
    // post_data.longitude = data.longitude;
    app.ihttpPost(data.add_url, post_data, function (res) {
      wx.hideLoading();
      if (res.data.err_code === 400) {
        wx.showModal({
          title: '提示',
          content: res.data.err_msg,
          showCancel: false
        })
      } else if (res.data.err_code === 0) {
        wx.showModal({
          title: '提示',
          content: res.data.err_msg,
          showCancel: false,
          success: function (res) {
            wx.redirectTo({
              url: data.index_url
            })
          }
        })
      } else {
        wx.requestPayment({
          'appId': res.data.appId,
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showModal({
                title: '提示',
                content: "支付成功！",
                showCancel: false,
                success: function (res) {
                  wx.redirectTo({
                    url: data.index_url
                  })
                }
              })
            }
          }
        })
      }
    })
  },
})